"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库tinyint数据类型
Case Name   : mot表使用tinyint类型
Create At   : 2022.11.17
Owner       : z@wan005
Description :
    1、建mot表
    2、插入数据，覆盖左右边界
    3、插入0
    4、插入小数
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功，四舍五入
    5、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest0021(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_tinyint_0039"
        self.tb_name = "tb_tinyint_0039"
        self.default_value = self.common.show_param(
            'enable_incremental_checkpoint')

    def test_tinyint(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：设置参数为off并创建兼容b库---')
        sql_res = self.sh_primary.execut_db_sql('alter system set '
                                                'enable_incremental_checkpoint'
                                                ' to off;')
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, sql_res)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:建mot表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop foreign table if exists {self.tb_name};" \
                  f"create foreign table {self.tb_name}(c1 tinyint);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('CREATE FOREIGN TABLE', sql_res, '执行失败' + text)

        text = '--step2:插入数据，覆盖左右边界;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values(-128);" \
                  f"insert {self.tb_name} values(127);" \
                  f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         '执行失败' + text)
        self.assertIn('-128', sql_res, '执行失败' + text)
        self.assertIn('127', sql_res, '执行失败' + text)

        text = '--step3:插入0;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values(0);" \
                  f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_res, '执行失败' + text)
        self.assertIn('0', sql_res, '执行失败' + text)

        text = '--step4:插入小数;expect:成功，四舍五入--'
        self.log.info(text)
        sql_cmd = f"truncate table {self.tb_name};" \
                  f"insert {self.tb_name} values(-128.4);" \
                  f"insert {self.tb_name} values(-127.9);" \
                  f"insert {self.tb_name} values(127.4);" \
                  f"insert {self.tb_name} values(126.5);" \
                  f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.INSERT_SUCCESS_MSG), 4,
                         '执行失败' + text)
        self.assertIn('TRUNCATE', sql_res, '执行失败' + text)
        self.assertEqual(sql_res.count('-128'), 2, '执行失败' + text)
        self.assertEqual(sql_res.count('-128'), 2, '执行失败' + text)
        self.assertEqual(sql_res.count(self.constant.INSERT_SUCCESS_MSG), 4,
                         '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop foreign table if exists {self.tb_name};"
        self.log.info(sql_cmd)
        drop_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                 dbname=f'{self.db_name}')
        self.log.info(drop_res)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        alter_cmd = self.sh_primary.execut_db_sql(
            f'alter system set enable_incremental_checkpoint to'
            f' {self.default_value};')
        self.log.info(alter_cmd)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, alter_cmd)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertIn('DROP FOREIGN TABLE', drop_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
